package com.etc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.MD5Util;
import com.etc.util.Message;
import com.etc.util.PageData;

@Controller
@RequestMapping(value = "userapi")
public class UserController {
	@Resource
	private UserService us;

	@Resource(name = "message")
	private Message msg;

	private Logger log = Logger.getLogger(UserController.class);

	/**
	 * 删除的方法
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "user/{userid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteByPrimaryKey(@PathVariable(value = "userid") int userid,HttpSession session) {
		log.info("删除的方法");
		//增加了管理员不能自己删自己得功能
		User user=us.selectByPrimaryKey(userid);
		if(session.getAttribute("username")!=null&&session.getAttribute("username").equals(user.getUsername())) {
			msg.setMsg("该删除操作违规");	
		}else {
			boolean flag = us.deleteByPrimaryKey(userid);
			if (flag) {
				msg.setMsg("删除成功");
			} else {
				msg.setMsg("删除失败");
			}
		}
		return msg;
	}

	/**
	 * 添加的方法
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "user", method = RequestMethod.POST)
	@ResponseBody
	public Message insertSelective(@RequestBody User user) {
		System.out.println("增加"+user);
		boolean flag = us.insertSelective(user);
		if (flag) {
			msg.setMsg("添加成功");
		} else {
			msg.setMsg("添加失败");
		}
		return msg;
	}

	/**
	 * 查询单个用户的方法
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "user/{userid}", method = RequestMethod.GET)
	@ResponseBody
	public User selectByPrimaryKey(@PathVariable(value = "userid") int userid) {
		log.info("查询单个");
		User user = us.selectByPrimaryKey(userid);
		return user;
	}

	/**
	 * 修改的方法
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "user", method = RequestMethod.PUT)
	@ResponseBody
	public Message updateByPrimaryKeySelective(@RequestBody User user,HttpSession session) {
		boolean flag = us.updateByPrimaryKeySelective(user);
		//判断session的名字是否和修改后的用户名一致
		if(session.getAttribute("username").equals(user.getUsername())) {
			if (flag) {
				msg.setMsg("修改成功");
			} else {
				msg.setMsg("修改失败");
			}
		}else {
			msg.setMsg("修改成功，请重新登录");
		}
		
		return msg;
	}

	@RequestMapping(value = "user", method = RequestMethod.GET)
	@ResponseBody
	public PageData<User> selectByPage(int page, int limit, String content) {
		if (content == null) {
			content = "";
		}
		PageData<User> pg = us.selectByPage(page, limit, content);
		pg.setCode(0);
		pg.setMsg("");
		return pg;
	}
	
	/**
	 * 后台管理员登录校验的方法
	 * cyf
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "adminlogin", method = RequestMethod.GET)
	@ResponseBody
	public Message selectByUser(User user,HttpServletRequest request) {
		String pwd = MD5Util.getEncodeByMd5(user.getUserpwd());
		user.setUserpwd(pwd);
		//log.info("查询单个");
		boolean flag = us.selectByUser(user);
		if (flag) {
			request.getSession().setAttribute("username", user.getUsername());
			msg.setMsg("校验成功");
		} else {
			msg.setMsg("校验失败");
		}
		return msg;
	}
	
	/**
	 * 前后台登录框失焦事件
	 * cyf
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "adminlogin2", method = RequestMethod.GET)
	@ResponseBody
	public Message selectByUsername(String username) {
		msg.setMsg("");
		//log.info("查询单个");
		System.out.println(username);
		boolean flag = us.selectByUsername(username);
		if (!flag) {
			msg.setMsg("该用户不存在");
		} 
		return msg;
	}

	/**
	 * 前端登录校验的方法
	 * cyf
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "frontlogin", method = RequestMethod.GET)
	@ResponseBody
	public Message checkByUser(User user,HttpServletRequest request) {
		String pwd = MD5Util.getEncodeByMd5(user.getUserpwd());
		user.setUserpwd(pwd);
		//log.info("查询单个");
		User User = us.checkByUser(user);
		if (User!=null) {
			request.getSession().setAttribute("userid", User.getUserid());
			request.getSession().setAttribute("username", User.getUsername());
			request.getSession().setAttribute("birthday", User.getBirthday());
			request.getSession().setAttribute("tel", User.getTel());
			request.getSession().setAttribute("sex", User.getSex());
			request.getSession().setAttribute("email", User.getEmail());
			msg.setMsg("校验成功");
		} else {
			msg.setMsg("校验失败");
		}
		return msg;
	}
	/**
	 * 前端登录控制器
	 * cyf
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "tofrontlogin")
	public String  tofrontlogin() {
		
		return "frontlogin";
	}
	/**
	 * 前端界面控制器
	 * cyf
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "tohome")
	public String  tohome() {
		
		return "home";
	}
	
	/**
	 * 注册的方法
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "zhuce", method = RequestMethod.POST)
	@ResponseBody
	public Message insertselective(User user) {
		boolean flag = us.insertuser(user);
		if (flag) {
			msg.setMsg("添加成功");
		} else {
			msg.setMsg("添加失败");
		}
		return msg;
	}
	
	@RequestMapping(value="checksession")
	public String checksession(HttpSession session) {
		
		if (session.getAttribute("username")!=null) {
			return "redirect:/admin/views/information.jsp";
		} else {
			return "frontlogin";
		}
		
	}
}
